﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller;
using Entity;
using System.Drawing;
namespace Web
{
    public partial class CheckPurchaseHistory : System.Web.UI.Page
    {
        QueryController queryController = new QueryController();
        Helper helper = new Helper();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IList<PurchaseHistoryEntity> purchaseHistoryEntities = queryController.findAllPurchaseHistory();
                PurchaseHistoryGridview.DataSource = purchaseHistoryEntities;
                PurchaseHistoryGridview.DataBind();
                foreach (GridViewRow r in PurchaseHistoryGridview.Rows)
                {
                    if (r.Cells[7].Text == "Late")
                    {
                        r.ForeColor = Color.Red;
                    }
                }

                VendorController vendorController = new VendorController();
                IList<VendorEntity> vendorEntities = vendorController.FindAllVendorEntities();
                VendorDropDownList.DataSource = vendorEntities;
                VendorDropDownList.DataValueField = "Id";
                VendorDropDownList.DataTextField = "ShortName";
                VendorDropDownList.DataBind();
                VendorDropDownList.Items.Insert(0, new ListItem("All Vendors", "0"));
            }
            this.PreRender += new EventHandler(UpdateFlashMessage);
        }

        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }

        protected void PurchaseHistoryGridview_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            PurchaseHistoryGridview.PageIndex = e.NewPageIndex;

            GetResultEntitiesByConsitions();

        }

        protected void ShowLateButton_Click(object sender, EventArgs e)
        {
            IList<PurchaseHistoryEntity> purchaseHistoryEntities = queryController.findLatePurchaseHistory();
            PurchaseHistoryGridview.DataSource = purchaseHistoryEntities;
            PurchaseHistoryGridview.DataBind();
            foreach (GridViewRow r in PurchaseHistoryGridview.Rows)
            {
                if (r.Cells[7].Text == "Late")
                {
                    r.ForeColor = Color.Red;
                }
            }
        }

        

        protected void GetResultEntitiesByConsitions()
        {
            string productNo = ProductNoTextBox.Text.Trim().ToUpper();
            string poNo = PONoTextBox.Text.Trim().ToUpper();
            int vendorId = Convert.ToInt32(VendorDropDownList.SelectedValue);
            DateTime from = (FromTextBox.Text.Length > 0) ? Convert.ToDateTime(FromTextBox.Text) : DateTime.MinValue;
            DateTime to = (ToTextBox.Text.Length > 0) ? Convert.ToDateTime(ToTextBox.Text) : DateTime.MinValue;
            IList<PurchaseHistoryEntity> purchaseHistoryEntities =
                    queryController.FindPurchaseHistoryByConditions(productNo,poNo,vendorId,from,to);
            PurchaseHistoryGridview.DataSource = purchaseHistoryEntities;
            PurchaseHistoryGridview.DataBind();
            foreach (GridViewRow r in PurchaseHistoryGridview.Rows)
            {
                if (r.Cells[7].Text == "Late")
                {
                    r.ForeColor = Color.Red;
                }
            }
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            GetResultEntitiesByConsitions();
        }
    }
}
